import {
  treeCourseCataloglist,
  courseDetail
} from '@/api/course/course'
import {
  countCourseBuyByCourseId
} from '@/api/buy.js'
import {
  get_img_src
} from '@/utils/index.js'
import CourseChapterDetail from '@/components/Course/CourseChapterDetail/index.vue'
export default {
  components: {
    CourseChapterDetail
  },
  data() {
    return {
      courseId: this.$route.query.courseId || '',
      treeOptions: {
        align: 'left',
        show_checkbox: false,
        actFrom: this.$route.query.actFrom
      },
      info: {},
      infoNum: 0,
      purchaseCount: 0,
      loading: null,
      loadingCount: 3
    }
  },
  created() {
    this.handleHttp()
  },
  methods: {
    // 获取购买数量
    countCourseBuyByCourseId() {
      this.loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      countCourseBuyByCourseId({
        courseId: this.courseId
      }).then(res => {
        if (res.code === 20000) {
          this.purchaseCount = res.data
        } else {
          this.$message(res.msg)
        }
        this.loading.close()
      }).catch(error => {
        this.$message.error('获取购买数量失败' + error)
      })
    },

    // 获取课程目录
    getTreeCourseCataloglist() {
      const _this = this
      treeCourseCataloglist({
        courseCatalogId: '',
        courseId: this.courseId,
        courseCatalogName: '',
        courseCatalogPid: ''
      }).then(res => {
        if (res.code === 20000) {
          console.log(res.data, '课程目录')
          ++this.loadingCount
          if (_this.loadingCount >= this.loadingCount) {
            this.loadingCount = 0
            this.loading.close()
          }
        } else {
          this.loading.close()
          this.$message(res.msg)
        }
      }).catch(error => {
        this.$message.error('获取课程目录失败!' + error)
        this.loading.close()
      })
    },

    // 获取课程详情
    getCourseDetail() {
      const _this = this
      courseDetail({
        courseId: this.courseId
      }).then(res => {
        if (res.code === 20000) {
          this.info = res.data
          ++this.loadingCount
          if (_this.loadingCount >= this.loadingCount) {
            this.loadingCount = 0
            this.loading.close()
          }
        } else {
          this.$message(res.msg)
          this.loading.close()
        }
      }).catch(error => {
        this.$message.error('获取课程详情失败' + error)
        this.loading.close()
      })
    },

    // 统一发送请求
    handleHttp() {
      this.countCourseBuyByCourseId()
      // this.getTreeCourseCataloglist();
      this.getCourseDetail()
    },
    // 购买月数
    handleChange(value) {
      console.log(value)
    },
    // 分享
    share() {

    },
    // 收藏
    collect() {

    },
    get_img_src(val) { // 获取图片完整地址
      return get_img_src(val)
    }
  }
}
